using System;
using Genrsis.WorkitemTracking.Commands;
using Genrsis.WorkitemTracking.Domain;
using Raven.Client;

namespace Genrsis.WorkitemTracking.Handlers.Workflows
{
    public class CreateWorkflowCommandHandler : ICommandHandler<CreateWorkflowCommand>
    {
        private readonly IDocumentStore _documentStore;

        public CreateWorkflowCommandHandler(IDocumentStore documentStore)
        {
            _documentStore = documentStore;
        }

        public void Handle(CreateWorkflowCommand command)
        {
            using (var session = _documentStore.OpenSession())
            {
                var user = session.Load<User>(command.UserId);

                var workflow = user.CreateWorkflow(command.Name);
                session.Store(workflow);

                session.SaveChanges();
            }
        }
    }
}